package com.stone.business.plan.action;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.business.plan.pojo.TBzgzrw;
import com.stone.sys.pojo.TBzgsk;
import com.stone.sys.service.SysCommonService;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;

public class BzgzrwRwAction extends BaseListAction {
	
	public HashMap<String, Object> setSearchCondition() {
		return null;
	}

	private static final long serialVersionUID = 865527685459140622L;

	private SysCommonService myService;
	
	private TGzrwdRw entity;
	
	private String id;
	private String sbnames;
	private String sblxid;
	private String bid;
	private String paramPlanid;
	private String paramBzid;
	private String paramSbid;
	private String paramGzrwdid;
	private String paramSbname;
	public void getBzgzrwList() throws IOException, AppException {
		
		//
		String conditions = " where 1 = 1";
		String sql = 	"select a.id as id,(select organ_name from sys_organ b where b.id = a.bzid) as bzname," +
						" (select item_name from sys_dictionary c where c.item_id = a.dydj and c.item_type = 'DYDJ') as dydj," +
						" (select item_name from sys_dictionary d where d.item_id = a.sblx and d.item_type = 'SBLX') as sblx," +
						" a.gznr as gznr,a.bzgs as bzgs,a.bzid as bid,a.sblx as sblxid,a.sbname as sbname" +
						" from TAB_GZRWD_RW a";
		/*if(!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())) {
			conditions += " and a.bzid = '" + super.getSearchBean().getCon1() + "'";
		}*/
		
		/*if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND a.dydj = '" + super.getSearchBean().getCon2() + "'";
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND a.sblx = '" + super.getSearchBean().getCon3() + "'";
		}*/
		
		if(!StringUtils.isNullOrBlankFull(paramBzid)){
			conditions += " and a.bzid = '" + paramBzid + "'";
		}
		if(!StringUtils.isNullOrBlankFull(paramPlanid)){
			conditions += " and a.planid = '" + paramPlanid + "'";
		}
		if(!StringUtils.isNullOrBlankFull(paramGzrwdid)){
			conditions += " and a.gzrwdid = '" + paramGzrwdid + "'";
		}
		sql += conditions;
		
		List list = myService.findListBySql(sql);
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strjson = "{result:" + json.toString() + ",totalCount:[" + list.size() + "]}";
		
        super.getResponse().getWriter().print(strjson);
		
	}
	
	public void save() throws IOException, AppException {
		
		if (entity.getId() == 0) {
			myService.save(entity);
		}else{
			String dydj = "";
			if(!StringUtils.isNullOrBlankFull(entity.getDydj())){
				dydj = entity.getDydj();
				if(dydj.indexOf('k')!=-1){
					dydj = dydj.substring(0, dydj.indexOf('k'));
					entity.setDydj(dydj);
				}
			}
			if(!StringUtils.isNullOrBlankFull(sblxid)){
				entity.setSblx(sblxid);
			}
			if(!StringUtils.isNullOrBlankFull(bid)){
				entity.setBzid(Long.parseLong(bid));
			}
			myService.update(entity);
		}
		super.getResponse().getWriter().print("{success:true,msg:'保存成功',id:'" + entity.getId() + "'}");
	}
			
	@SuppressWarnings("unchecked")
	public void checkout() throws IOException,AppException{
		
		if(id!=null&&!"".equals(id)){
			String[] ids = id.split(",");
			String conditions = " where 1 = 1";
			String sql = 	"select a.id as id,(select organ_name from sys_organ b where b.id = a.bzid) as bzname,a.bzid as bzid," +
							" (select item_id from sys_dictionary c where c.item_id = a.dydj and c.item_type = 'DYDJ') as dydj," +
							" (select item_id from sys_dictionary d where d.item_id = a.sblx and d.item_type = 'SBLX') as sblx," +
							" a.zymc as gznr,a.bzgs as bzgs" +
							" from tab_bzgsk a";
			if(ids.length == 1){
				conditions += " and id = '" + ids[0] + "'";
			}else{
				conditions += " and id in (" + id + ")";
			}
			sql += conditions;
			
			List list = myService.findListBySql(sql);
			if(list!=null&&list.size()>0){
				HashMap map = new HashMap();
				for(int i=0;i<list.size();i++){
					map = (HashMap) list.get(i);
					entity = new TGzrwdRw();
					entity.setBzid(((BigDecimal)map.get("bzid")).longValue());
					//entity.setBzgs(map.get("bzgs").toString());
					entity.setDydj((String) map.get("dydj"));
					entity.setSblx((String) map.get("sblx"));
					entity.setGznr((String) map.get("gznr"));
					entity.setPlanid(Long.parseLong(paramPlanid));
					entity.setSbid(Long.parseLong(paramSbid));
					entity.setGzrwdid(Long.parseLong(paramGzrwdid));
					entity.setSbname(paramSbname);
					myService.save(entity);
				}
			}
			super.getResponse().getWriter().print("'提取成功'");
			
		}
	}
//////////////////////by wangtao add20120828
	@SuppressWarnings("unchecked")
	public void saveMulti() throws IOException,AppException{
		
		if(id!=null&&!"".equals(id)){
			String[] ids = id.split(",");
			String[] sbname = sbnames.split(",");
				for(int i=0;i<ids.length;i++){
					TGzrwdRw bzgzrw = new TGzrwdRw();
					bzgzrw.setBzid(entity.getBzid());
					bzgzrw.setBzgs(entity.getBzgs());
					bzgzrw.setDydj(entity.getDydj());
					bzgzrw.setSblx(entity.getSblx());
					bzgzrw.setGznr(entity.getGznr());
					bzgzrw.setPlanid(Long.parseLong(paramPlanid));
					bzgzrw.setGzrwdid(Long.parseLong(paramGzrwdid));
					bzgzrw.setSbid(Long.parseLong(ids[i]));
					bzgzrw.setSbname(sbname[i]);
					myService.save(bzgzrw);
				}
				//super.getResponse().getWriter().print("'多个设备保存成功'");
				super.getResponse().getWriter().print("{success:true,msg:'多个设备保存成功',id:'" + entity.getId() + "'}");
				
			}
			
		
	}
	//////////////////////////////////////////////////////
public void delete() throws Exception {
		
		myService.doDelAll(id, TGzrwdRw.class.getName());
		
		super.getResponse().getWriter().print("删除成功");
		
	}
	public SysCommonService getMyService() {
		return myService;
	}

	public void setMyService(SysCommonService myService) {
		this.myService = myService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public TGzrwdRw getEntity() {
		return entity;
	}

	public void setEntity(TGzrwdRw entity) {
		this.entity = entity;
	}


	public String getSblxid() {
		return sblxid;
	}

	public void setSblxid(String sblxid) {
		this.sblxid = sblxid;
	}

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getParamPlanid() {
		return paramPlanid;
	}

	public void setParamPlanid(String paramPlanid) {
		this.paramPlanid = paramPlanid;
	}

	public String getParamBzid() {
		return paramBzid;
	}

	public void setParamBzid(String paramBzid) {
		this.paramBzid = paramBzid;
	}

	public String getParamSbid() {
		return paramSbid;
	}

	public void setParamSbid(String paramSbid) {
		this.paramSbid = paramSbid;
	}

	public String getParamSbname() {
		return paramSbname;
	}

	public void setParamSbname(String paramSbname) {
		this.paramSbname = paramSbname;
	}

	public String getParamGzrwdid() {
		return paramGzrwdid;
	}

	public void setParamGzrwdid(String paramGzrwdid) {
		this.paramGzrwdid = paramGzrwdid;
	}
	public String getSbnames() {
		return sbnames;
	}

	public void setSbnames(String sbnames) {
		this.sbnames = sbnames;
	}
}
